package com.baihui.taobao.record;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.baihui.crm.DES;
import com.baihui.taobao.db.DBConnectionMgr;

public class PayUser {

	public String taobaoNick = "";
	public String subNick = "";
	public String baihuiId = "";
	public String email = "";
	public String password = "";
	public String subpw = "";
	public String appName = "";
    public String createTime;
    // 授权状态 0:过期，1：有效
    public String accreditStu = "";
	public String getAppName() {
		return appName;
	}

	public void setAppName(String appName) {
		this.appName = appName;
	}

	// 是否实施 0:没有，1：完成
	public int status = 0;

	public String getSubpw() {
		return subpw;
	}

	public void setSubpw(String subpw) {
		this.subpw = subpw;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getSubNick() {
		return subNick;
	}

	public void setSubNick(String subNick) {
		this.subNick = subNick;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getTaobaoNick() {
		return taobaoNick;
	}

	public void setTaobaoNick(String taobaoNick) {
		this.taobaoNick = taobaoNick;
	}

	public String getBaihuiId() {
		return baihuiId;
	}

	public void setBaihuiId(String baihuiId) {
		this.baihuiId = baihuiId;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public String getCreateTime() {
		if(createTime != null && !"".equals(createTime)){
			createTime = createTime.substring(0,19);
		}
		return createTime;
	}

	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}

	public String getAccreditStu() {
		return accreditStu;
	}

	public void setAccreditStu(String accreditStu) {
		this.accreditStu = accreditStu;
	}

	public static List<PayUser> findAll(String userType) {

		Connection cn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;

		List<PayUser> payUserList = new ArrayList<PayUser>();
		try {
			cn = DBConnectionMgr.getConnection();
			cn.setAutoCommit(true);
			String sql = "";
			if("1".equals(userType)){
				sql = "select tp.* from topsession tp , ttriggle tt where tp.baihuiuser = tt.userid and tt.jobid = 2 order by tp.createTime desc ";
			}else{
				sql = "select * from topsession where baihuiuser is not null order by createTime desc ";
			}
			ps = cn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				PayUser payUser = new PayUser();
				payUser.setBaihuiId(rs.getString("baihuiuser"));
				payUser.setTaobaoNick(rs.getString("nick"));
				payUser.setEmail(rs.getString("baihuimail"));
				payUser.setSubNick(rs.getString("sub_visitor_nick"));
				payUser.setPassword(DES.decodePw(rs.getString("parentbaihuipw")));
				payUser.setSubpw(DES.decodePw(rs.getString("subpw")));
				payUser.setStatus(Integer.parseInt(rs.getString("status")));
				payUser.setAppName(rs.getString("appName"));
				payUser.setCreateTime(rs.getString("createTime"));
				if (rs.getString("sub_visitor_nick") == null
						&& !isSessionValid(Long.valueOf(rs.getString("ts")),
								Long.valueOf(rs.getString("expires_in")))) {
					
					payUser.setAccreditStu("过期");
				}else{
					payUser.setAccreditStu("有效");
				}
				
				payUserList.add(payUser);
			}

		} catch (Exception exception) {
			exception.printStackTrace();
		} finally {
			releaseDBConnection(cn, ps, rs);
		}
		return payUserList;
	}

	// 如何判断Session是否有效
	public static boolean isSessionValid(long ts, long expires_in) {
		return System.currentTimeMillis() < (ts + expires_in * 1000l);
	}
	
	/**
	 * 释放数据库连接
	 */
	private static void releaseDBConnection(Connection cn,
			PreparedStatement ps, ResultSet rs) {
		try {
			if (rs != null)
				rs.close();
			if (ps != null)
				ps.close();
			if (cn != null)
				cn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
